/**
 * 方块
 * by wyy
 */
class brick extends GameObject implements eui.UIComponent {
    private data = {
        mass: 1,
        position: [500, 500],
        width: 1,
        height: 1,
        bgColor: 0xffffff
    };
    public lifeLbl: eui.Label;

    public type: string = "brick";
    public bg: eui.Rect;

    public phyBody: p2.Body;
    private lifeNum: number = 99;
    private lifeMax: number = 99;
    constructor({ mass = 0, position = [640, 360], width = 1, height = 1, lifeMax = 1, bgColor = "green" }) {
        super();
        this.skinName = "resource/eui_skins/game_skins/brickSkin.exml";
        this.data.mass = mass;
        this.data.position = position;
        this.data.width = width;
        this.data.height = height;
        this.lifeMax = lifeMax;
        this.data.bgColor = gameScene.getInstance().gameColor[bgColor];
    }
    firstCreate() {
        let phy = physicsUtil.getInstance();
        let data = this.data;
        let positionX = data.position[0] / phy.phyFactor;
        let positionY = (egret.MainContext.instance.stage.stageHeight - data.position[1]) / phy.phyFactor;
        let phyShape = new p2.Box({ width: this.width / phy.phyFactor, height: this.height / phy.phyFactor, color: 0xffffff });
        phyShape.material = phy.brickMat;
        this.phyBody =
            new p2.Body({ mass: data.mass, position: [positionX, positionY] });
        this.phyBody.addShape(phyShape);
        this.phyBody.displays = [this];
        this.anchorOffsetX = this.width / 2;
        this.anchorOffsetY = this.height / 2;
        phy.phyWorld.addBody(this.phyBody);
        this.phyBody.damping = 0;
        this.phyBody.angularDamping = 0;

        //显示生命值
        this.lifeNum = this.lifeMax;
        this.lifeLbl.text = this.lifeMax.toString();
        //背景颜色
        this.bg.fillColor = this.data.bgColor;
    }
    addToStage() {

    }
    onRemove() {

    }
    hited() {
        this.lifeNum -= 1;
        this.lifeLbl.text = this.lifeNum.toString();
        if (this.lifeNum <= 0) {
            this.removeFromParent();
            physicsUtil.getInstance().phyWorld.removeBody(this.phyBody);
        }
    }
    setPos(x, y) {
        let globalP = this.parent.localToGlobal(x, y);
        let [rX, rY] = physicsUtil.getInstance().posEgretToP2(globalP.x, globalP.y);
        this.phyBody.position = [rX, rY];
        this.lifeNum = this.lifeMax;
        //显示生命值
        this.lifeLbl.text = this.lifeNum.toString();
    }
}